<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        a {
            text-decoration: none;

        }

        .login-header {
            padding: 10px;
            text-align: center;
            font-size: 24px;
            cursor: pointer;
        }

        .login-header a {
            color: #000;
            font-weight: 700;
            cursor: pointer;
        }

        .login {
            display: none;
            position: relative;
            width: 500px;
            height: 300px;
            background-color: #fff;
            margin: 100px auto;
            box-shadow: 1px 1px 5px 1px #fff;
            z-index: 1;
        }

        .login-title {
            font-size: 16px;
            text-align: center;
            padding: 20px;
            font-size: 16px;
            font-weight: 500;
            cursor: move;
        }

        .close-login {
            position: absolute;
            top: -20px;
            right: -20px;
            width: 40px;
            height: 40px;
            line-height: 40px;
            border-radius: 50%;
            color: #000;
            font-size: 14px;
            font-weight: normal;
            background-color: #fff;
            z-index: 0;
        }

        .login-input-content {
            padding-top: 15px;
        }

        .login-input {

            margin-bottom: 20px;
            margin-right: 30px;
            text-align: right;
        }

        .login-input label {
            font-size: 14px;
        }

        .user,
        .password {
            padding-left: 10px;
            width: 350px;
            height: 30px;
            border: 1px solid #ccc;
        }

        .user:focus,
        .password:focus {
            outline-color: #a2c1f6;
        }

        .login-button {
            width: 250px;
            height: 40px;
            line-height: 40px;
            border: 1px solid #ccc;
            margin: 10px auto;
            text-align: center;
            margin-top: 50px;
        }

        .login-button a {
            color: #000;
            font-size: 16px;
        }

        .login-bg {
            display: none;
            width: 100%;
            height: 100%;
            position: fixed;
            top: 0px;
            left: 0px;
            background: rgba(0, 0, 0, .3);
        }
    </style>
</head>

<body>
    <div class="login-header"><a id="link" href="javascript:;">点击，弹出登录框</a></div>
    <div id="login" class="login">
        <div id="title" class="login-title">登录会员
            <span><a id="closeBtn" href="javascript:void(0);" class="close-login">关闭</a></span>
        </div>
        <div class="login-input-content">
            <div class="login-input">
                <label>用户名：</label>
                <input type="text" placeholder="请输入用户名" name="info[username]" id="username" class="user">
            </div>
            <div class="login-input">
                <label>登录密码：</label>
                <input type="password" placeholder="请输入登录密码" name="info[username]" id="password" class="password">
            </div>
        </div>
        <div id="loginBtn" class="login-button"><a href="javascript:void(0);" id="login-button-submit">登录会员</a></div>
    </div>
    <!-- 遮盖层 -->
    <div id="bg" class="login-bg"></div>
    <script>
        var login = document.querySelector('.login');
        var mask = document.querySelector('.login-bg');
        var link = document.querySelector('#link');
        var closeBtn = document.querySelector('#closeBtn');
        var title = document.querySelector('#title');
        //2.点击弹出这个链接 link  让mask 和login 显示出来
        link.addEventListener('click', function () {
            mask.style.display = 'block';
            login.style.display = 'block';
        });
        //3.点击这个按钮让 mask 和login  隐藏起来
        closeBtn.addEventListener('click', function () {
            mask.style.display = 'none';
            login.style.display = 'none';
        });
        //4.开始拖拽
        // （1）当鼠标按下， 就获得鼠标在盒子内的坐标
        title.addEventListener('mousedown', function (e) {
            var x = e.pageX - login.offsetLeft;
            var y = e.pageY - login.offsetTop;
            // (2) 鼠标移动的时候，把鼠标在页面中的坐标， 减去 鼠标在盒子内的坐标
            document.addEventListener('mousemove', move);
            function move(e) {
                login.style.left = e.pageX - x - 525 + 'px';
                login.style.top = e.pageY - y - 150 + 'px';
            }

            // (3) 鼠标弹起 就让鼠标移动事件移除
            document.addEventListener('mouseup', function () {
                document.removeEventListener('mousemove', move);
            });
        });
    </script>
</body>

</html>